<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class goods_model extends base_model {

	public function __construct() {
		parent::__construct();
	}

	//产品列表
	public function get_list($id) {
		$sql = "SELECT * FROM jf_goods WHERE status=1 AND id='{$id}'";
		$row = $this->db->get_row($sql);
		return $row;
	}

	//获取产品详情
	public function detail($goods_id, $show_origin_pic=false) {
		$sql = "SELECT g.* 
			FROM jf_goods g 
			WHERE g.status='1' AND g.id='{$goods_id}'";
		//echo $sql;
		$row = $this->db->get_row($sql);
		if ($row) {
			$row = add_domain_deep($row, array("default_pic"));
			$row->pics = $this->get_pics($goods_id, $show_origin_pic);
			$html = '';
			if ($row->pics) {
				foreach ($row->pics as $pics)  {
					$pic = add_domain($pics->pic);
					//$html .= "<img src='{$pic}' />";
					//if ($pics->memo) $html .= "<p>{$pics->memo}</p>";
				}
			}
			//$row->intro = $html;
			$row->specs = $this->get_specs($goods_id, true);
			//$row->country = $this->get_country($row->country_id);
			//$row->sale_method_name = $this->sale_method_name($row->sale_method);
			//$row->favorited = $this->favorited($goods_id, $this->member_id);
		}
		return $row;
	}

	//该商品是否已经收藏
	public function favorited($goods_id, $member_id) {
		$favorited = 0;
		if ($member_id) {
			$sql = "SELECT count(*) FROM jf_favorite WHERE item_id='{$goods_id}' AND member_id='{$member_id}'";
			$favorited = intval($this->db->get_var($sql));
		}
		return $favorited;
	}

	//获取商品的产地
	public function get_country($country_id) {
		if ($country_id) {
			$sql = "SELECT id, name, flag_pic FROM jf_country WHERE id='{$country_id}'";
			$row = $this->db->get_row($sql);
			return $row;
		}
	}

	//强制获取产品详情，不用判断是否下架
	public function enforce_get_detail($goods_id, $show_origin_pic=false) {
		$sql = "SELECT g.* 
			FROM jf_goods g 
			WHERE g.id='{$goods_id}'";
		$row = $this->db->get_row($sql);
		if ($row) {
			$row = add_domain_deep($row, array("default_pic"));
			$row->pics = $this->get_pics($goods_id, $show_origin_pic);
			$html = '';
			if ($row->pics) {
				foreach ($row->pics as $pics)  {
					$pic = add_domain($pics->pic);
					$html .= "<img src='{$pic}' />";
					if ($pics->memo) $html .= "<p>{$pics->memo}</p>";
				}
			}
			$row->intro = $html;
			$row->specs = $this->get_specs($goods_id, true);
		}
		return $row;
	}

	//获取商品的图片
	public function get_pics($goods_id, $show_origin_pic=false) {
		$sql = "SELECT pic, memo FROM jf_goods_pic WHERE goods_id='{$goods_id}' ORDER BY id ASC";
		$pics = $this->db->get_results($sql);
		$pics = add_domain_deep($pics, array("pic"));
		//为商品增加缩略图
		if (!$show_origin_pic && $pics) {
			$num = count($pics);
			if ($num==1) {
				$size = "big";
			} elseif ($num=2) {
				$size = "medium";
			} else {
				$size = "small";
			}
			foreach ($pics as $k=>$p) {
				if (strpos($p->pic, '!') !== FALSE) continue;
				$pics[$k]->pic = get_upyun_thumb_url($p->pic, $size);
			}
		}
		return $pics;
	}


	//规格
	//20160120 by ajsong 因为APP界面原因,需要检测是否只有一条规格记录且名称为默认规格,是的话就返回空, $allways_show不检测是否只有一条记录
	public function get_specs($goods_id, $allways_show=false) {
		if (!$allways_show) {
			$sql = "SELECT count(*) FROM jf_goods_spec WHERE goods_id='{$goods_id}'";
			$count = $this->db->get_var($sql);
			if ($count==1) {
				$sql = "SELECT spec FROM jf_goods_spec WHERE goods_id='{$goods_id}'";
				$spec = $this->db->get_var($sql);
				if ($spec == '默认规格') {
					return array();
				}
			}
		}
		$sql = "SELECT * FROM jf_goods_spec WHERE goods_id='{$goods_id}' ORDER BY id ASC";
		$specs = $this->db->get_results($sql);
		return $specs;
	}

	//获取某个规格下的价格
	public function get_spec_price($goods_id, $spec) {
		$sql = "SELECT price FROM jf_goods_spec WHERE goods_id='{$goods_id}' AND spec='{$spec}'";
		$price = floatval($this->db->get_var($sql));
		return $price;
	}

	//状态
	public function status_name($status) {
		$name = $status;
		switch ($status) {
			case 0:
				$name = '下架';
				break;
			case 1:
				$name = '正常';
				break;
		}
		return $name;
	}	

	//销售方式：1，保税直发，2：海外直采
	public function sale_method_name($sale_method) {
		$name = '';
		switch ($sale_method) {
			case 1:
				$name = '保税直发';
				break;
			case 2:
				$name = '海外直采';
				break;
		}
		return $name;
	}
}
